/****************************************************************************************
   Gender Norms — IV mediation for Table 9 (All / Women / Men) with IPW weights
****************************************************************************************/

*/****************************************************************************************
   Gender Norms — IV mediation for Table 9 (All / Women / Men) with IPW weights
****************************************************************************************/

********************************************************************************
* 0) Paths, scheme, and dataset
********************************************************************************
clear all
set more off
version 18
set seed 12051985

* ---- Choose route ----
global dir "replication"   

if "$dir"=="ana" {
    global root   = "/Users/adiazescobar/Library/CloudStorage/Dropbox/GenderNorms2/Results/"
    global output = "/Users/adiazescobar/Library/CloudStorage/Dropbox/GenderNorms2/Results/Analysis/Final/Tables Mediaton"
}
if "$dir"=="ana_imac" {
    global root   = "/Users/adaizescobar/Dropbox/Gender Norms2/Results"
    global output = "/Users/adaizescobar/Dropbox/Gender Norms2/Results"
}
if "$dir"=="marie" {
    global root   = "C:\Users\Marie Boltz\Dropbox\Gender Norms2\Results\"
    global output = "C:\Users\Marie Boltz\Dropbox\Gender Norms2\Results\"
}

if "$dir"=="replication" {
    global root   = "."
    global output = "."
    global datadir = "./data"
    global codedir = "./code"
    global outdir  = "./output"
    capture mkdir "$outdir"
}

cd "$output"

* Graph defaults (QJE/AER-ish)
set scheme s2mono
graph set window fontface "Times New Roman"

* Load master data
use "$datadir/dataALLWAVES.dta", clear

* Remove inconsistent gender at wave 3
drop if female_w3 != female & female_w3 != .

* Main sample indicator
local sample sample3_ext

********************************************************************************
* 1) Beliefs, strata, baseline controls
********************************************************************************

* Belief blocks
local bel1 fob1 sob1 sob1sdb sob1m 
local bel2 fob2 sob2 sob2m sob2p
local bel3 fob3 sob3 sob3sdb sob3m sob3p
local bel4 fob4 fob6
local bel5 fob5 sob5 sob5m sob5p
local bel6 fob7 sob7 sob7m sob7p
local bel7 fob8 sob8 sob8m sob8p
local bel8 e501 e502 e503 e501b e504 e505 e506 e504b
local bel9 fob9

* All belief variables used in selection model
local bels `bel1' `bel2' `bel3' `bel4' `bel6' `bel7' `bel8' `bel9'

* Strata covariates (absorbed FE in main models elsewhere; here as controls)
local strat inactiveF employedF fob2M

* Outcomes used elsewhere (not central here)
local yvars fob2 sob2 sob2m DplurM2 DplurF2 

* Baseline controls (female included)
local xvars female c1 c2_2_rp c3 c5 f6 age_dif  level_1 level_2 ///
            edu_WmoreH inactiveM unemployedM estrato_12 estrato_3 tec_2 

* Diagnostics for selective exposure (optional)
reghdfe D `xvars' `bel2' if `sample' == 1, absorb(`strat') vce(cluster id)

* Working control set for main outcome models (if needed)
local vars_sample3 `xvars' `bel2' 

********************************************************************************
* 2) Common definitions for mediation
********************************************************************************

* Main sample indicator and strata
local sample   sample3_ext
local strat    inactiveF employedF fob2M

* Baseline controls WITHOUT female (for within-gender regressions)
local xvars_base c1 c2_2_rp c3 c5 f6 age_dif level_1 level_2 ///
                 edu_WmoreH inactiveM unemployedM estrato_12 estrato_3 tec_2

* Outcomes of Table 9
* Only job_search_123_w3 has a baseline (job_search_123)
local yvars9  job_search_123_w3 metas_laboral_w3_2 metas_laboral_w3_3
local ylags9  job_search_123       .                    .

* Mediators (endline beliefs)
* We will use sob2_w3 and sob2m_w3 as mediators
local medlist sob2 sob2m   

********************************************************************************
* 3) IPW weights for selection into sample 3 (sample3_ext)
********************************************************************************

* Probit for P(sample3_ext = 1 | baseline)
capture noisily probit `sample' D `xvars' `bels' `strat', vce(cluster id)

predict ps_s3, pr
gen double w_ipw3 = 1/ps_s3
local wvar w_ipw3

********************************************************************************
* 4) IV Mediation – WOMEN, weighted
********************************************************************************

eststo clear
cap log close
log using "$output/mediation_table9_IV_women.log", replace

local k_out = 1
foreach y of local yvars9 {

    local y0 : word `k_out' of `ylags9'
    local ylagopt ""
    if "`y0'" != "." local ylagopt "`y0'"

    foreach med of local medlist {

        local med_w3 `med'_w3
        local controls `xvars_base' `bel2' `ylagopt'

        di as txt "===================================================="
        di as txt "WOMEN – Outcome: `y'   |  Mediator: `med_w3'   |  Baseline: `ylagopt'"
        di as txt "===================================================="

        *----------- (1) Reduced form: D → Y -----------
        reghdfe `y' D `controls' [pw = `wvar'] ///
            if `sample' == 1 & female == 1, ///
            absorb(`strat') vce(cluster id)

        scalar rf_b_D  = _b[D]
        scalar rf_se_D = _se[D]

        *----------- (2) First stage: D → M -----------
        reghdfe `med_w3' D `controls' [pw = `wvar'] ///
            if `sample' == 1 & female == 1, ///
            absorb(`strat') vce(cluster id)

        test D
        scalar fs_F_D  = r(F)
        scalar fs_pi_D = _b[D]

        *----------- (3) 2SLS: Y on M (M instrumented by D) -----------
        ivreg2 `y' (`med_w3' = D) `controls' `strat' ///
            [pw = `wvar'] if `sample' == 1 & female == 1, ///
            cluster(id) first

        scalar iv_alpha  = _b[`med_w3']
        scalar med_comp  = iv_alpha * fs_pi_D
        scalar med_share = .
        if abs(rf_b_D) > 1e-6 scalar med_share = med_comp / rf_b_D

        *----------- Display diagnostics (for the log) -----------
        di as txt "RF β(D→Y)       = " %6.3f rf_b_D  " (se " %6.3f rf_se_D ")"
        di as txt "First-stage π   = " %6.3f fs_pi_D "  |  F = " %6.2f fs_F_D
        di as txt "2SLS α(M→Y)     = " %6.3f iv_alpha
        di as txt "Mediated comp   = " %6.3f med_comp "  |  share = " %6.3f med_share
        di as txt " "

        *----------- Attach diagnostics to this IV regression -----------
        estadd scalar rf_b_D    = rf_b_D
        estadd scalar rf_se_D   = rf_se_D
        estadd scalar fs_pi_D   = fs_pi_D
        estadd scalar fs_F_D    = fs_F_D
        estadd scalar med_comp  = med_comp
        estadd scalar med_share = med_share

        *----------- Store IV regression for esttab (top panel) -----------
        local estname W`k_out'_`med'
        eststo `estname'
    }

    local ++k_out
}

log close

* Export IV table for WOMEN (mediator coefficients + SEs + diagnostics)
esttab using "$output/Table9_Mediation_IV_Women.tex", replace ///
    keep(sob2_w3 sob2m_w3) ///
    cells(b(star fmt(3)) se(fmt(3) par)) ///
    scalars("rf_b_D    RF: β(D→Y)" ///
            "med_comp  Mediated" ///
            "med_share Share mediated" ///
            "fs_pi_D   First-stage π" ///
            "fs_F_D    First-stage F") ///
    star(* 0.10 ** 0.05 *** 0.01) ///
    label nonumber noobs 

********************************************************************************
* 5) IV Mediation – POOLED SAMPLE (ALL), weighted
********************************************************************************

eststo clear
cap log close
log using "$output/mediation_table9_IV_all.log", replace

* Controls now include female
local xvars_all female `xvars_base'

local k_out = 1
foreach y of local yvars9 {

    local y0 : word `k_out' of `ylags9'
    local ylagopt ""
    if "`y0'" != "." local ylagopt "`y0'"

    foreach med of local medlist {

        local med_w3 `med'_w3
        local controls `xvars_all' `bel2' `ylagopt'

        di as txt "===================================================="
        di as txt "ALL – Outcome: `y'   |  Mediator: `med_w3'   |  Baseline: `ylagopt'"
        di as txt "===================================================="

        *----------- (1) Reduced form: D → Y -----------
        reghdfe `y' D `controls' [pw = `wvar'] ///
            if `sample' == 1, ///
            absorb(`strat') vce(cluster id)

        scalar rf_b_D  = _b[D]
        scalar rf_se_D = _se[D]

        *----------- (2) First stage: D → M -----------
        reghdfe `med_w3' D `controls' [pw = `wvar'] ///
            if `sample' == 1, ///
            absorb(`strat') vce(cluster id)

        test D
        scalar fs_F_D  = r(F)
        scalar fs_pi_D = _b[D]

        *----------- (3) 2SLS: Y on M (M instrumented by D) -----------
        ivreg2 `y' (`med_w3' = D) `controls' `strat' ///
            [pw = `wvar'] if `sample' == 1, ///
            cluster(id) first

        scalar iv_alpha  = _b[`med_w3']
        scalar med_comp  = iv_alpha * fs_pi_D
        scalar med_share = .
        if abs(rf_b_D) > 1e-6 scalar med_share = med_comp / rf_b_D

        *----------- Display diagnostics (for the log) -----------
        di as txt "RF β(D→Y)       = " %6.3f rf_b_D  " (se " %6.3f rf_se_D ")"
        di as txt "First-stage π   = " %6.3f fs_pi_D "  |  F = " %6.2f fs_F_D
        di as txt "2SLS α(M→Y)     = " %6.3f iv_alpha
        di as txt "Mediated comp   = " %6.3f med_comp "  |  share = " %6.3f med_share
        di as txt " "

        *----------- Attach diagnostics to this IV regression -----------
        estadd scalar rf_b_D    = rf_b_D
        estadd scalar rf_se_D   = rf_se_D
        estadd scalar fs_pi_D   = fs_pi_D
        estadd scalar fs_F_D    = fs_F_D
        estadd scalar med_comp  = med_comp
        estadd scalar med_share = med_share

        *----------- Store IV regression for esttab -----------
        local estname A`k_out'_`med'
        eststo `estname'
    }

    local ++k_out
}

log close

esttab using "$output/Table9_Mediation_IV_All.tex", replace ///
    keep(sob2_w3 sob2m_w3) ///
    cells(b(star fmt(3)) se(fmt(3) par)) ///
    scalars("rf_b_D    RF: β(D→Y)" ///
            "med_comp  Mediated" ///
            "med_share Share mediated" ///
            "fs_pi_D   First-stage π" ///
            "fs_F_D    First-stage F") ///
    star(* 0.10 ** 0.05 *** 0.01) ///
    label nonumber noobs 

********************************************************************************
* 6) IV Mediation – MEN, weighted
********************************************************************************

eststo clear
cap log close
log using "$output/mediation_table9_IV_men.log", replace

local k_out = 1
foreach y of local yvars9 {

    local y0 : word `k_out' of `ylags9'
    local ylagopt ""
    if "`y0'" != "." local ylagopt "`y0'"

    foreach med of local medlist {

        local med_w3 `med'_w3
        local controls `xvars_base' `bel2' `ylagopt'

        di as txt "===================================================="
        di as txt "MEN – Outcome: `y'   |  Mediator: `med_w3'   |  Baseline: `ylagopt'"
        di as txt "===================================================="

        *----------- (1) Reduced form: D → Y -----------
        reghdfe `y' D `controls' [pw = `wvar'] ///
            if `sample' == 1 & female == 0, ///
            absorb(`strat') vce(cluster id)

        scalar rf_b_D  = _b[D]
        scalar rf_se_D = _se[D]

        *----------- (2) First stage: D → M -----------
        reghdfe `med_w3' D `controls' [pw = `wvar'] ///
            if `sample' == 1 & female == 0, ///
            absorb(`strat') vce(cluster id)

        test D
        scalar fs_F_D  = r(F)
        scalar fs_pi_D = _b[D]

        *----------- (3) 2SLS: Y on M (M instrumented by D) -----------
        ivreg2 `y' (`med_w3' = D) `controls' `strat' ///
            [pw = `wvar'] if `sample' == 1 & female == 0, ///
            cluster(id) first

        scalar iv_alpha  = _b[`med_w3']
        scalar med_comp  = iv_alpha * fs_pi_D
        scalar med_share = .
        if abs(rf_b_D) > 1e-6 scalar med_share = med_comp / rf_b_D

        *----------- Display diagnostics (for the log) -----------
        di as txt "RF β(D→Y)       = " %6.3f rf_b_D  " (se " %6.3f rf_se_D ")"
        di as txt "First-stage π   = " %6.3f fs_pi_D "  |  F = " %6.2f fs_F_D
        di as txt "2SLS α(M→Y)     = " %6.3f iv_alpha
        di as txt "Mediated comp   = " %6.3f med_comp "  |  share = " %6.3f med_share
        di as txt " "

        *----------- Attach diagnostics to this IV regression -----------
        estadd scalar rf_b_D    = rf_b_D
        estadd scalar rf_se_D   = rf_se_D
        estadd scalar fs_pi_D   = fs_pi_D
        estadd scalar fs_F_D    = fs_F_D
        estadd scalar med_comp  = med_comp
        estadd scalar med_share = med_share

        *----------- Store IV regression for esttab -----------
        local estname M`k_out'_`med'
        eststo `estname'
    }

    local ++k_out
}

log close

esttab using "$output/Table9_Mediation_IV_Men.tex", replace ///
    keep(sob2_w3 sob2m_w3) ///
    cells(b(star fmt(3)) se(fmt(3) par)) ///
    scalars("rf_b_D    RF: β(D→Y)" ///
            "med_comp  Mediated" ///
            "med_share Share mediated" ///
            "fs_pi_D   First-stage π" ///
            "fs_F_D    First-stage F") ///
    star(* 0.10 ** 0.05 *** 0.01) ///
    label  nonumber noobs
